﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoEarth.Entities;
using MonoEarth.GISExtensions;
using MonoGame.Extended.ViewportAdapters;
using System;

namespace MonoEarth
{
    public class GameEarth : Game
    {

        public ViewportAdapter ViewportAdapter { get; private set; }


        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;

        private Texture2D background;
        private Texture2D shuttle;
        private Texture2D earth;


        private QGeometry _currentDemo;
        /// <summary>
        /// 鼠标状态
        /// </summary>
        public static MouseStateEnum MouseStateEnumType {  get;set; }
        public SceenPosition SceenDrawPosition { get; set; }

        public GameEarth()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
           _graphics.PreferHalfPixelOffset = false;
            _graphics.PreferredBackBufferWidth =800;
            _graphics.PreferredBackBufferHeight = 450;
            ViewportAdapter = new BoxingViewportAdapter(this.Window, _graphics.GraphicsDevice, _graphics.PreferredBackBufferWidth, _graphics.PreferredBackBufferHeight);
            _currentDemo = new QPoint(this);

        }

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
            this.Window.Title = "数字地球";
            
            
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            background = Content.Load<Texture2D>("images/stars"); // change these names to the names of your images
            shuttle = Content.Load<Texture2D>("images/shuttle");  // if you are using your own images.
            earth = Content.Load<Texture2D>("images/earth");

            _currentDemo.Load();

           
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            var mouseState = Mouse.GetState();//获取鼠标状态
            if(mouseState.LeftButton == ButtonState.Pressed)
            {
                MouseStateEnumType = MouseStateEnum.Line;
                SceenDrawPosition = new SceenPosition();
                SceenDrawPosition.IsFirst=true;
                SceenDrawPosition.FirstPoint = mouseState.Position;

                (_currentDemo as QPoint).X = mouseState.X;
                (_currentDemo as QPoint).Y = mouseState.Y;
            }
            else
            {
                //SceenDrawPosition.SecondPoint= mouseState.Position;
            }
            

            // TODO: Add your update logic here

            _currentDemo?.OnUpdate(gameTime);



            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            _spriteBatch.Begin();
            _spriteBatch.Draw(background, new Rectangle(0, 0, _graphics.PreferredBackBufferWidth, _graphics.PreferredBackBufferHeight), Color.White);
            _spriteBatch.Draw(earth, new Vector2(400, 240), Color.White);
            _spriteBatch.Draw(shuttle, new Vector2(450, 240), Color.White);

            _spriteBatch.End();

            // TODO: Add your drawing code here

            if(SceenDrawPosition!=null && SceenDrawPosition.IsFirst)
            {
                _spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive);

                

                _spriteBatch.End();

            }

            _currentDemo?.OnDraw(gameTime);

            base.Draw(gameTime);
        }





    }
}